﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.Design;

namespace Super.Admin.Assets.CustomControl
{
    [Designer(typeof(CenteredPanelDesigner))]
    public partial class CenterPanel : Panel
    {
        private bool _centerHorizontally = true;
        private bool _centerVertically = true;

        public CenterPanel()
        {
            this.AutoScroll = false;
            this.ResizeRedraw = true;
            this.DoubleBuffered = true;
        }

        [Category("Layout")]
        [DefaultValue(true)]
        [Description("是否水平居中子控件")]
        public bool CenterHorizontally
        {
            get => _centerHorizontally;
            set
            {
                if (_centerHorizontally != value)
                {
                    _centerHorizontally = value;
                    CenterChildControl();
                    Invalidate();
                }
            }
        }

        [Category("Layout")]
        [DefaultValue(true)]
        [Description("是否垂直居中子控件")]
        public bool CenterVertically
        {
            get => _centerVertically;
            set
            {
                if (_centerVertically != value)
                {
                    _centerVertically = value;
                    CenterChildControl();
                    Invalidate();
                }
            }
        }

        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);
            CenterChildControl();
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            // 如果已经有子控件，移除新添加的控件
            if (this.Controls.Count > 1)
            {
                this.Controls.Remove(e.Control);
                MessageBox.Show("此容器只能包含一个子控件", "警告",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            CenterChildControl();
        }

        protected override void OnLayout(LayoutEventArgs levent)
        {
            base.OnLayout(levent);
            CenterChildControl();
        }

        private void CenterChildControl()
        {
            if (this.Controls.Count == 0)
                return;

            var child = this.Controls[0];
            var newLocation = Point.Empty;

            if (_centerHorizontally)
                newLocation.X = (this.ClientSize.Width - child.Width) / 2;

            if (_centerVertically)
                newLocation.Y = (this.ClientSize.Height - child.Height) / 2;

            child.Location = newLocation;
        }

        // 重写Controls属性以隐藏设计时不需要的方法
        [Browsable(false)]
        public new ControlCollection Controls
        {
            get => base.Controls;
        }
    }

    // 设计器类，用于在设计时提供更好的体验
    public class CenteredPanelDesigner : ParentControlDesigner
    {
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            var centeredPanel = component as CenterPanel;
            if (centeredPanel != null)
            {
                // 启用设计时绘制
                this.EnableDesignMode(centeredPanel, "CenteredContainer");
            }
        }

        // 允许将控件拖放到容器中
        public override bool CanParent(Control control)
        {
            // 只允许在容器为空时添加控件
            return ((Control)this.Component).Controls.Count == 0;
        }

        // 限制只能添加一个控件
        public override bool CanParent(ControlDesigner controlDesigner)
        {
            return ((Control)this.Component).Controls.Count == 0;
        }
    }
}
